# -*- coding: utf-8 -*-
from datetime import datetime, date, time
from decimal import Decimal
import json

import py_global
import logging

logger = logging.getLogger(__name__)


class DefaultEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        if isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        if isinstance(obj, time):
            return obj.strftime('%H:%M:%S')
        if isinstance(obj, Decimal):
            return float(obj)
        else:
            return super(DefaultEncoder, self).default(obj)


def py_json():
    var_dict: dict = {
        'str_field': '10',
        'int_field': 10,
        'float_field': 0.1,
        'decimal_field': Decimal('0.1'),
        'bool_field': True,
        'none_field': None,
        'datetime_field': datetime.now(),
        'date_field': datetime.now().date(),
        'time_field': datetime.now().time(),
        'list_field': [
            23,
            '23',
        ],
    }

    # 处理json字符串
    # var_str = json.dumps(var_dict, default=str)
    var_str = json.dumps(var_dict, cls=DefaultEncoder)
    var_object = json.loads(var_str)
    logger.info(var_object)

    # # 处理json文件
    # with open('./json.json', 'w+') as file:
    #     json.dump(var_object, file)
    #     pass
    # with open('./json.json', 'r+') as file:
    #     json_dict = json.load(file)
    #     pass

    pass


if __name__ == '__main__':
    py_json()
